
<template>
  <div style="margin: 20px; overflow-y: auto">
    <h1>{{ id ? "编辑" : "新建" }}广告位</h1>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      style="margin-top: 30px"
      @submit.native.prevent
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="ruleForm.items.push({})" style="margin-bottom: 20px">
          <i class="el-icon-plus"></i>
          添加广告
        </el-button>
        <el-row type="flex" style="flex-wrap: wrap">
          <el-col :md="24" v-for="(item, index) of ruleForm.items" :key="index">
            <el-form-item label="跳转链接（URL）">
              <el-input v-model="item.url"></el-input>
            </el-form-item>
            <el-form-item label="图片">
              <el-upload
                class="avatar-uploader"
                :action="uploadsUrl"
                :headers="getAuthHeaders()"
                :show-file-list="false"
                :on-success="(res) => $set(item, 'image', res.url)"
              >
                <img v-if="item.image" :src="item.image" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item>
              <el-button
                size="small"
                type="danger"
                @click="ruleForm.skills.splice(index, 1)"
              >
                删除
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: ["id"],
  data() {
    return {
      ruleForm: {
        name: "",
        items: [],
      },
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    fetch() {
      this.id &&
        this.$http.get("rest/ads/" + this.id).then((res) => {
          if (res.status === 200) {
            this.ruleForm = Object.assign({}, this.ruleForm, res.data);
          }
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http
            .post(
              "rest/ads" + (this.id ? `/${this.id}` : ""),
              this.id ? { id: this.id, ...this.ruleForm } : this.ruleForm
            )
            .then((res) => {
              if (res.status === 200) {
                this.$router.push("/ads/list");
                this.$message({
                  type: "success",
                  message: "保存成功",
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  min-width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  min-width: 80px;
  height: 80px;
  display: block;
}
.el-form-item .el-form-item {
  margin-bottom: 16px;
}
</style>
